//
//  MCNewMarketingAddGoodsCell.swift
//  StoreManage
//
//  Created by MC on 2018/6/27.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit


class MCNewMarketingAddGoodsCell: UITableViewCell {
    
    var row = 0
    
    var model : MCNewMarketingAddGoodsModel = MCNewMarketingAddGoodsModel() {
        didSet {
            iconImageView.sd_setImage(with: URL.init(string: model.navigateImg1), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
//            nameLabel.text = model.goodsName
//            switch model.mallStoreId.intValue {
//            case 0:  // 平台
//                sourceLabel.text = "平台"
//            default:
//                sourceLabel.text = "自营"
//            }
            
            
            if model.mallStoreId.intValue != 0 {
                nameLabel.attributedText = model.goodsName.MCAttributedString_textAttachment(text: model.goodsName, image: #imageLiteral(resourceName: "GoodsManage_ziying").MCImageWithTintMainColor(), imageFrame: CGRect(x: 0, y: -4, width: 60*0.6, height: 28*0.6))
            } else {
                nameLabel.text = model.goodsName
            }

            
            

            selectedButton.isSelected = model.isSelected
            moneyLabel.text = MCString_judgemantDecimalsIsZero(figure: model.lowestPrice)
            
            inventoryLabel.text = "库存总计:" + model.inventoryNum.stringValue

        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = UIColor.MCGray_light
        
        self.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(15, 15, 0, 15))
        }
        
        bgView.addSubview(selectedButton)
        selectedButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(0)
            make.centerY.equalTo(bgView)
            make.size.equalTo(CGSize.init(width: 40, height: 40))
        }
        
        bgView.addSubview(iconImageView)
        iconImageView.snp.remakeConstraints { (make) ->Void in
            make.centerY.equalTo(bgView)
            make.left.equalTo(selectedButton.snp.right)
            make.size.equalTo(CGSize.init(width: 70, height: 70))
        }
        
        
        bgView.addSubview(moneyLabel)
        moneyLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.bottom.equalTo(iconImageView)
        }
        
        bgView.addSubview(inventoryLabel)
        inventoryLabel.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(bgView).offset(-10)
            make.centerY.equalTo(moneyLabel)
        }


        bgView.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.top.equalTo(iconImageView)
            make.right.equalTo(bgView).offset(-5)
            make.bottom.lessThanOrEqualTo(moneyLabel.snp.top).offset(-2)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func selectedButtonClicked() {
        selectedButton.isSelected = !selectedButton.isSelected
        model.isSelected = selectedButton.isSelected
    }
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 4
        view.layer.masksToBounds = true
        return view
    }()
    
    lazy var selectedButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_notSelected"), for: UIControlState.normal)
        button.setImage(#imageLiteral(resourceName: "GoodsManage_selected"), for: UIControlState.selected)
        button.addTarget(self, action: #selector(selectedButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        return imageView
    }()
    
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.numberOfLines = 0
        label.font = MCFont(13)
        return label
    }()
    
    lazy var moneyLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.MCMain
        label.font = MCFont(13)
        return label
    }()
    
    lazy var inventoryLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.black
        label.font = MCFont(12)
        return label
    }()
    
}
